/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.siagefi.controller;

import br.siagefi.model.Contrato;
import br.siagefi.service.ContratoService;
import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

/**
 *
 * @author Paulo Lavarini
 */
@ManagedBean
@SessionScoped
public class ContratoBean implements Serializable{
    
    private Contrato contrato;
    private final ContratoService service;
    private List<Contrato> contratos;

    public ContratoBean() {
        this.contrato = new Contrato();
        this.service = new ContratoService();
        this.carregarLista();
    }
    
     public final void carregarLista(){
         this.contratos = service.listar();
    }
    
    public String gravar(){
        service.gravar(this.getContrato());
        this.limparFormularioDoJsf();
        this.carregarLista();
        return "contrato?faces-redirect=true";
    }
    
    public String alterar(Contrato contrato){
        this.setContrato(contrato);
        return "incluirContrato";
    }
    
     public String remover(){
        service.remover(this.getContrato());
        this.limparFormularioDoJsf();
        this.carregarLista();
        return "contrato";
    }
     
    public String remover(Contrato contrato){
        service.remover(contrato);
        this.limparFormularioDoJsf();
        this.carregarLista();
        return "contrato";
    }
  
    public void limparFormularioDoJsf(){
        this.contrato = new Contrato();
    }

    public Contrato getContrato() {
        return contrato;
    }

    public void setContrato(Contrato contrato) {
        this.contrato = contrato;
    }

    public List<Contrato> getContratos() {
        return contratos;
    }

    public void setContratos(List<Contrato> contratos) {
        this.contratos = contratos;
    }
}
